using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;

namespace Ordina.SharePoint.RemoveShareFollow.Features.FeatureRemoveSF
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("fc8b6c8d-d718-4c53-961a-4bd6042af517")]
    public class FeatureRemoveSFEventReceiver : SPFeatureReceiver
    {
        private string logClass = "FeatureRemoveSFEventReceiver";
        private const string key = "ShowHideSF";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SetValuePropertyBag(properties, "true");
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SetValuePropertyBag(properties, "false");
        }

        private void SetValuePropertyBag(SPFeatureReceiverProperties properties, string value)
        {
            string logMethod = "SetValuePropertyBag";

            SPSite currentSite = null;
            SPWeb currentWeb = properties.Feature.Parent as SPWeb;
            if (currentWeb == null)
            {
                currentSite = properties.Feature.Parent as SPSite;
                currentWeb = currentSite.RootWeb;
            }
            else
            {
                currentSite = currentWeb.Site;
            }

            bool allowUnsafeUpdates = currentWeb.AllowUnsafeUpdates;
            currentWeb.AllowUnsafeUpdates = true;

            if (!currentWeb.Properties.ContainsKey(key))
                currentWeb.Properties.Add(key, value);
            else
                currentWeb.Properties[key] = value;

            currentWeb.Properties.Update();

            currentWeb.AllowUnsafeUpdates = allowUnsafeUpdates;
        }
    }
}
